<template>
  <div class="login-box" id="index-loginbox">
    <div class="loginbg" style="overflow-x: hidden">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="账号密码登录" name="first">
          <LoginForm ref="loginForm" @save="loginAction"></LoginForm>
        </el-tab-pane>

        <el-tab-pane label="手机号登录" name="second" @click="tabPlane">
          <el-form ref="loginPhoneForm" :model="loginPhoneForm" :rules="rules" style="height: 380px">
            <el-form-item prop="phone">
              <el-input clearable prefix-icon="el-icon-phone" class="text-style" v-model="loginPhoneForm.phone" placeholder="请输入手机号"></el-input>
            </el-form-item>
            <div style="display: flex; align-items: center; position: relative">
              <el-form-item prop="code" style="flex: 1">
                <el-input prefix-icon="el-icon-yanzheng" class="text-style" v-model="loginPhoneForm.code" placeholder="短信验证码" clearable></el-input>
              </el-form-item>
              <el-form-item class="getyanzhengma" style="width: 120px">
                <el-button :disabled="codeDisable" type="primary" style="margin-left: 10px; width: 100px; font-size: 20px; text-align: center" @click="getCode(loginPhoneForm.phone)">{{
                  count
                }}</el-button>
              </el-form-item>
            </div>
            <el-form-item>
              <div class="submit">
                <el-button type="primary" @click="loginBtn">登 录</el-button>
                <!-- <el-button type="primary" style="width:100%" @click="l">l</el-button>
                <el-button type="primary" style="width:100%" @click="c">c</el-button>-->
              </div>
            </el-form-item>
          </el-form>
          <div class="sub-title">
            <div>
              <a @click="toReg">注册账号</a>
            </div>
            <div>
              <a>子用户登录</a>
            </div>
            <div class="inline">
              <a>忘记密码</a>
            </div>
          </div>
        </el-tab-pane>
      </el-tabs>

      <FootInfos></FootInfos>
    </div>
  </div>
</template>

<style lang="less" scoped>
/deep/.el-input__icon {
  margin-left: 8px;
}
/deep/ .el-icon-phone {
  background: url('../../assets/login02.png') center center no-repeat;
  background-size: 30px;
}
/deep/ .el-icon-phone:before {
  content: '替';
  font-size: 16px;
  visibility: hidden;
}

/deep/ .el-icon-yanzheng {
  background: url('../../assets/login01.png') center center no-repeat;
  background-size: 30px;
}
/deep/ .el-icon-yanzheng:before {
  content: '替';
  font-size: 16px;
  visibility: hidden;
}
@media screen and (max-width: 1900px) {
  html,
  body {
    overflow: hidden;
  }
  @import './index2.less';

  /deep/.el-input__icon {
    background-size: 22px;
    margin-left: 5px;
  }
}
@media screen and (min-width: 1900px) {
  @import './index.less';
  /deep/ .el-tabs__active-bar {
    height: 0px;
  }
  /deep/.el-icon-view:before {
    font-size: 25px;
    vertical-align: middle;
  }
  /deep/ .el-tabs__nav-wrap {
    overflow: hidden;
    margin-bottom: -1px;
    position: relative;
    width: 600px;
    margin-left: -60px;
    border-bottom: 7px solid #409eff;
  }
  /deep/.el-tabs__nav {
    margin-left: 60px;
  }

  /deep/ .el-form {
    margin-top: 30px;
    width: 480px;
  }
  /deep/ .el-form-item__error {
    height: 40px;
    line-height: 40px;
    width: 480px;
    font-size: 20px;
  }
  /deep/ .el-form-item {
    width: 480px;
    height: 80px;
    margin-bottom: 40px;
  }
  #tab-first {
    margin-right: 20px;
  }
  /deep/ .el-tabs__active-bar {
    width: 230px;
    height: 0px;
  }
  /deep/ .el-tabs__item {
    height: 70px;
    line-height: 70px;
    width: 230px;
    background-color: #fff;
    color: black;
    float: left;
    text-align: center;
    font-size: 24px;
    padding: 0;
    margin-right: 20px;
    border-radius: 40px 40px 0 0;
  }
  /deep/ .el-tabs__item .is-active {
    height: 70px;
    line-height: 70px;
    width: 230px;
    background-color: #409eff;
    color: white;
    float: left;
    text-align: center;
    font-size: 28px;
    border-radius: 50px 50px 0 0;
  }
  /deep/ .el-tabs__nav-wrap::after {
    background-color: #409eff;
    height: 0px;
  }

  /deep/ .el-tabs__item.is-active {
    color: white;
    background-color: #409eff;
  }
  /deep/ .sub-title {
    display: flex;
    justify-content: space-between;
    margin-top: -5px;
    color: white;
    height: 30px;
    line-height: 30px;
    margin: 5px 20px;
    margin-bottom: 50px;
    font-size: 22px;
  }

  .login-box .sub-title /deep/ {
    color: white;
    margin-top: 0px;
  }
  /deep/ #index-loginbox .el-form-item {
    width: 480px;
    height: 80px;
    margin-bottom: 10px;
  }
  /deep/ .el-form-item__content {
    height: 80px;
  }
  /deep/ .el-form-item .el-input {
    height: 80px;
  }
  /deep/ .el-input__inner {
    height: 80px;
    font-size: 24px;
    border-radius: 10px;
  }

  /deep/ .el-icon-user:before {
    content: '\e6e3';
    font-size: 30px;
    line-height: 80px;
  }
  /deep/ .el-input__icon {
    width: 50px;
  }
  /deep/ .el-input--prefix .el-input__inner {
    padding-left: 70px;
  }
  /deep/ .submit {
    width: 480px;
  }
  /deep/ .submit .el-button {
    height: 100px;
    background-color: #409eff;
    margin-top: 20px;
    border-radius: 50px;
    font-size: 30px;
    margin-bottom: 20px;
    width: 480px;
  }
  #index-loginbox .getyanzhengma {
    position: absolute;
    top: 5px;
    left: 320px;
    height: 80px;
    line-height: 80px;
  }
  #index-loginbox .getyanzhengma .el-button {
    background-color: #fff;
    color: #409eff;
    border: 0px;
    font-size: 30px;
    height: 70px;
    width: 100px;
    vertical-align: middle;
  }
  #index-loginbox .footinfos {
    margin-top: 44px;
  }
  /deep/ .el-tabs__content {
    overflow: hidden;
    margin-top: 50px;
  }
}
</style>


<script>
import LoginForm from './loginform'
import FootInfos from './footinfos'
import { setStore } from '../../utils/store'
// import CryptoJS from '../../utils/CryptoJS'
import { Logins } from './login'
//import { login, cc } from '@/api/userhome/staff.js'
export default {
  data() {
    return {
      activeName: 'first',
      loginPhoneForm: {
        // phone: '18993326922', //手机号
        // code: '15515', // 用户输入的验证码
        phone: '',
        code: '',
        grant_type: 'smsCode',
        smsCode: '', //调用接口发送短信验证码接口获取
        captcha: true,
        randomStr: '', //此参数为调用获取验证码接口时传递的随机码参数captcha为false此参数无需传递
        scope: 'server',
      },
      rules: {
        // 验证规则登录
        phone: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          { min: 11, max: 11, message: '请输入正确的手机号码', trigger: 'blur' },
        ],
        code: [
          { required: true, message: '请输入手机验证码', trigger: 'blur' },
          { min: 4, max: 6, message: '请输入正确的验证码', trigger: 'blur' },
        ],
      },
      codeDisable: false,
      count: '获取验证码',
      timer: null,
      regTimer: 60,
    }
  },
  created() {
    //获取验证码时间缓存
    const localTime = window.localStorage.getItem('regTimer')
    if (localTime && localTime < 60 && localTime > 0) {
      this.regTimer = localTime
      this.count = localTime
      this.codeDisable = true
      this.getCodeAction()
    }
    if (localTime == 0) {
      window.localStorage.setItem('regTimer', 60)
    }
  },

  methods: {
    handleClick() {},
    loginAction() {
      // 登录模块admin123456测试账号
      setStore({
        name: 'logininfo',
        content: {
          token: 'Basic cGFyazpwYXJr',
        },
      }),
        // this.$store.commit('Setuser', this.token),
        this.$router.push({
          path: '/home/index',
        })
    },
    loginBtn() {
      this.$refs.loginPhoneForm.validate((valid) => {
        if (!valid) return
        this.login()
      })
    }, // 登录请求
    async login() {
      try {
        const { data } = await Logins(this.loginForm)
        if (data.code === 0) {
          // this.$message({ message: '登陆成功', type: 'success' })
          this.$notify({
          title: '成功',
          message: '这是一条成功的提示消息',
          type: 'success',
          offset:300
        });
          setStore({
            name: 'logininfo',
            content: {
              token: 'Basic cGFyazpwYXJr',
            },
          }),
            this.$store.commit('Setuser', data.data)
          this.$router.push({
            path: '/userHome',
          })
        } else {
          this.$message.error(data.msg)
          // this.loginForm.password=CryptoJS.decrypt(this.loginForm.password) //密码解密
        }
      } catch (error) {
        console.log(error)
      }
    },
    getCode(phone) {
      const phoneReg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
      if (!phoneReg.test(phone)) {
        return this.$message.error('请输入正确的手机号码')
      }
      this.$http.get('account/sysuser/verify-code/' + phone, { params: { type: '2' } }).then((res) => {
        const { data } = res
        if (data.code === 0) {
          this.codeDisable = true
          this.getCodeAction()
          this.$message.success(data.data)
        } else {
          return this.$message.error(data.msg)
        }
      })
    },
    // 验证码倒计时
    getCodeAction() {
      if (!this.timer) {
        this.count = this.regTimer
        this.timer = setInterval(() => {
          if (this.count > 0 && this.count <= this.regTimer) {
            this.count--
          } else {
            this.show = true
            clearInterval(this.timer)
            this.timer = null
            this.codeDisable = false
            this.count = '重新获取验证码'
            window.localStorage.setItem('regTimer', 60)
            this.regTimer = 60
          }
        }, 1000)
      }
    },
    toReg() {
      this.$router.push({ path: '/reg' })
    },
    tabPlane() {
      console.log(this.activeName)
    },
  },
  components: {
    LoginForm,
    FootInfos,
  },
}
</script>
